﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using JeanFish.Blog;
using JeanFish.Blog.BLL;
using JeanFish.Blog.Model;
using JeanFish.Blog.BLL.Relationships;

namespace JeanFish.UnitTest.Blog
{
    [TestClass]
    public class BlogInfoTest
    {
        private string blogName;
        private string blogDescription;
        private int userId;
        [TestInitialize]
        public void randomBlogName()
        {
            blogName = "Blog:"+Guid.NewGuid().ToString();
            blogDescription = "Description of " + blogName;
            userId = (int)DateTime.Now.Ticks;
        }
        [TestMethod]
        //[ExpectedException(typeof(BlogException))]
        public void BizBlogInfoTest()
        {
            BizBlogInfo biz = new BizBlogInfo();
            int id = 0;
            BlogInfo blog = biz.GetBlog(id);
            Assert.IsNull(blog);
            blog = biz.CreateBlog(blogName, blogDescription);
            Assert.IsTrue(blog.ID>0);
            var blog2 = biz.GetBlog(blog.ID);
            Assert.IsTrue(blog.ID == blog2.ID);
            Assert.IsTrue(blog.Name == blog2.Name);
            blog2.Delete();
            biz.UpdateBlog(blog2);
            blog2 = biz.GetBlog(blog2.ID);
            Assert.IsNull(blog2);
            blog = biz.CreateBlog(blogName, blogDescription);
        }
        [TestMethod]
        public void BlogInfoStatusTest()
        {
            BlogInfo blog = new BlogInfo(blogName,blogDescription);
            Assert.IsTrue(blog.Status == BlogStatus.Draft);
            blog.Active();
            Assert.IsTrue(blog.Status == BlogStatus.Active);
            blog.Pause();
            Assert.IsTrue(blog.Status == BlogStatus.Pause);
            blog.Inactive();
            Assert.IsTrue(blog.Status == BlogStatus.Inactive);
            blog.Delete();
            Assert.IsTrue(blog.Status == BlogStatus.Deleted);
        }
        [TestMethod]
        public void UserBlogsRelationshipTest()
        {
            //blogName = "test fixed blog name";
            IUser user = new DefaultUser(userId);
            BlogInfo blog = new BlogInfo(blogName, blogDescription);
            UserBlogs rel = new UserBlogs(user);
            rel.AddValue(blog);
            //rel.RemoveValue(blog);
            //rel.RemoveValue(v => v.ID <= 10);
            rel.Save();
            //再次保存也可以
            rel.Save();
        }
        [TestMethod]
        public void NewBlogUsersRelationshipTest()
        {
            BlogInfo blog = new BlogInfo(blogName, blogDescription);
            BlogUsers rel = new BlogUsers(blog);
            rel.AddValue(new DefaultUser(1));
            rel.RemoveValue(u => u.ID == 3);
            rel.Save();
        }
    }
}
